//
//  CZEmoticonKeyboardPageCell.swift
//  53.表情键盘新工程
//
//  Created by gzxzmac on 16/3/31.
//  Copyright © 2016年 gzxzmac. All rights reserved.
//

import UIKit

// 1.定义协议
protocol CZEmoticonKeyboardPageCellDelegate: NSObjectProtocol {
    // 2.定义方法
    /// 表情按钮点击的代理事件
    func emoticonKeyboardPageCell(cell: CZEmoticonKeyboardPageCell, didSelectEmoticon emoticon: CZEmoticonModel)
    
    /// 删除按钮点击的代理事件
    func didSelectDeleteButton()
}

class CZEmoticonKeyboardPageCell: UICollectionViewCell {
    
    // MARK: - 属性
    // 3.定义代理属性
    weak var delegate: CZEmoticonKeyboardPageCellDelegate?
    
    /// 表情模型数组
    var emoticons: [CZEmoticonModel]? {
        didSet {
            guard let emotionMoldes = emoticons else {
                print("emotionMoldes没有值")
                return
            }
            
            // 先隐藏所有的按钮
            for button in emoticonButtons {
                button.hidden = true
            }
            
            // 设置按钮的图片,要显示图片或emoji的按钮,就不隐藏
            for (index, emoticon) in emotionMoldes.enumerate() {
                // 获取每个模型
                // 找到对应的按钮
                let emoticonButton = emoticonButtons[index]
                emoticonButton.emoticon = emoticon
                emoticonButton.hidden = false
            }
        }
    }
    
    /// indexPath
    var indexPath: NSIndexPath? {
        didSet {
//            debugLabel.text = "第 \(indexPath!.section) 组,第 \(indexPath!.item) 个cell"
            
            // 只有第0组才显示最近表情label
            recentLabel.hidden = indexPath!.section != 0
        }
    }
    
    /// 记录20个表情按钮
    private var emoticonButtons: [CZEmoticonButton] = [CZEmoticonButton]()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }
    
    private func prepareUI() {
         /// 创建20个表情按钮
        addEmoticonButtons()
        
        /// 添加删除按钮
        contentView.addSubview(deleteButton)
        
        self.addSubview(recentLabel)
        
        recentLabel.translatesAutoresizingMaskIntoConstraints = false
        self.addConstraint(NSLayoutConstraint(item: recentLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: recentLabel, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -6))
    }
    
    /// 创建20个表情按钮
    private func addEmoticonButtons() {
        for _ in 0..<CZEmoticonNumberOfPage {
            let emoticonButton = CZEmoticonButton()
//            emoticonButton.backgroundColor = UIColor.random()
            
            emoticonButton.titleLabel?.font = UIFont.systemFontOfSize(32)
            
            contentView.addSubview(emoticonButton)
            
            emoticonButton.addTarget(self, action: "didClickEmoticonButton:", forControlEvents: UIControlEvents.TouchUpInside)
            
            emoticonButtons.append(emoticonButton)
        }
    }
    
    // MARK: - 按钮点击事件
    /// 表情按钮
    @objc private func didClickEmoticonButton(button: CZEmoticonButton) {
        // 4.调用代理的方法
        delegate?.emoticonKeyboardPageCell(self, didSelectEmoticon: button.emoticon!)
    }
    
    /// 删除按钮
    @objc private func didClickdeleteButton() {
        delegate?.didSelectDeleteButton()
    }
    
    /// 重新布局按钮
    private func relayoutButtons() {
        let marginLR: CGFloat = 3
        let marginBottom: CGFloat = 26
        
        // 计算宽度
        let width = (frame.width - 2 * marginLR) / CGFloat(CZEmoticonColumnOfPage)
        
        // 计算高度
        let height = (frame.height - marginBottom) / CGFloat(CZEmoticonRowOfPage)
        
        for (index, button) in contentView.subviews.enumerate() {
            // 计算按钮在哪一列
            let column = index % CZEmoticonColumnOfPage
            
            // 计算按钮在哪一行
            let row = index / CZEmoticonColumnOfPage
            
            let frame = CGRect(x: marginLR + CGFloat(column) * width, y: CGFloat(row) * height, width: width, height: height)
            
            button.frame = frame
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        relayoutButtons()
    }
    
    // MARK: - 懒加载
    /// 调试label
    private lazy var debugLabel: UILabel = {
        let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(18)
        label.textColor = UIColor.redColor()
        label.frame = CGRect(x: 0, y: 0, width: 300, height: 170)
        label.numberOfLines = 0
        
        self.addSubview(label)
        
        return label
    }()
    
    /// 删除按钮
    private lazy var deleteButton: UIButton = {
        let button = UIButton(type: UIButtonType.Custom)
        
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        button.addTarget(self, action: "didClickdeleteButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    /// 最近使用表情label
    private lazy var recentLabel: UILabel = {
        let label = UILabel()
        
        label.text = "最近使用表情"
        label.textColor = UIColor.lightGrayColor()
        label.font = UIFont.systemFontOfSize(12)
        label.sizeToFit()
        
        return label
    }()
}
